package graphicUserInterface.mainInterface;

import graphicUserInterface.mainInterface.screens.CalibrationScreen;
import graphicUserInterface.mainInterface.screens.FirstScreen;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;

public class MainWindows extends JFrame{

	/**
	 * 
	 */
	private static final long serialVersionUID = -8672198059694553247L;
	
	/* *********************************************************
	 * Attributes
	 * ********************************************************* */
	private JPanel _screenCommons;
	private JPanel _mainScreen;
	private FirstScreen _fstScreen;
	private CalibrationScreen _calibrationScreen;
	private JMenuBar _menuBar;
	private String _mainScreenPlace;
	//private Jsomething _ statusBar;
	
	/* *********************************************************
	 * Constructor(s)
	 * ********************************************************* */
	/**
	 * Default Constructor
	 * TODO : Add a link to the Engine's Thread as parameter...
	 */
	public MainWindows(){
		super();
		System.out.println("GUI:MainWindow creation");
		//first set the application initial parameters :
		this.setSize(800, 600);
		this.setTitle("Welcome in HINB");
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setResizable(false);
		//then start the initialisation
		System.out.println("screenCommons initialization ...");
		_screenCommons = initMainWindow();
		System.out.println("fstScreen initialization ...");
		_fstScreen = new FirstScreen(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				_calibrationScreen = new CalibrationScreen();
				_calibrationScreen.drawFstScreen();
				addAsMainScreen(_calibrationScreen);
			}

			
		});
		_fstScreen.draw();
		System.out.println("_fstScreen as mainScreen");
		addAsMainScreen(_fstScreen);
		this.setContentPane(_screenCommons);
		System.out.println("GUI:MainWindow creation over !");
	}
	
	/* *********************************************************
	 * Instance Methods
	 * ********************************************************* */
	
	/**
	 * remove the previous mainScreen, put the new one instead and 
	 * redraw the container.
	 * @param newMainScreen the new screen to show
	 */
	private synchronized void addAsMainScreen(JPanel newMainScreen) {
		System.out.println("GUI:MainWindow  addAsMainScreen");
		try {
			_screenCommons.remove(_mainScreen);
		} catch (NullPointerException e1) {
			System.out.println("no previous main screen nothing to " +
					"remove");
			// i.e. : _mainScreen is not Initialize yet
		}
		try{//we surround this by a try catch to be sure that a
			//programmer error will not crash the application :
			//i.e. if he/she give a bad name to _mainScreenPlace
			_screenCommons.add(newMainScreen, _mainScreenPlace);
		}catch (IllegalArgumentException e) {
			_screenCommons.add(newMainScreen);
		}finally{
			//no matter what happened, we need to redraw the screen
			_mainScreen = newMainScreen;
			_screenCommons.validate();
		}
	}
	
	/**
	 * add the menuBar, menu entries and all the global stuff 
	 * @return A JPanel containing all the useful commons things
	 */
	private JPanel initMainWindow() {
		System.out.println("GUI:MainWindow  initMainWindows");
		//create the screen
		JPanel screen = new JPanel();
		screen.setLayout(new BorderLayout());
		screen.setBackground(Color.LIGHT_GRAY);
		//add a MenuBar
		_menuBar = new JMenuBar();
		JMenu HINBMenu = new JMenu();
		HINBMenu.setText("HINB");
		HINBMenu.add(new JMenuItem("test", 'e'));
		_menuBar.add(HINBMenu);
		screen.add(_menuBar, BorderLayout.NORTH);
		_mainScreenPlace = BorderLayout.CENTER;
		//add other commons displays
		//nothing to add now maybe in more advance version
		return screen;
	}
	
}
